package com.chenbei.controller.admin.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.chenbei.common.util.ApiResponseUtil;
import com.chenbei.common.domain.vo.ApiResponse;
import com.chenbei.common.domain.vo.TreeNode;
import com.chenbei.system.domain.SysMenu;
import com.chenbei.system.service.ISysMenuService;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "菜单管理")
@RequestMapping("/admin/system/menu")
@RestController
public class MenuController {

    @Autowired
    private ISysMenuService menuService;

    @SaCheckPermission(value = "system:menu:add", orRole = "root")
    @Schema(description = "新增菜单")
    @PostMapping("add")
    public ApiResponse<Object> add(@RequestBody @Valid SysMenu menu) {
        menuService.add(menu);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:menu:delete", orRole = "root")
    @Schema(description = "删除菜单")
    @PostMapping("deleteById/{id}")
    public ApiResponse<Object> delete(@PathVariable Long id) {
        menuService.deleteById(id);
        return ApiResponseUtil.success();
    }

    @SaCheckPermission(value = "system:menu:update", orRole = "root")
    @Schema(description = "更新菜单")
    @PostMapping("update")
    public ApiResponse<Object> update(@RequestBody @Valid SysMenu menu) {
        menuService.update(menu);
        return ApiResponseUtil.success();
    }

    @Schema(description = "根据父级ID查询菜单列表")
    @GetMapping("listByPid")
    public ApiResponse<List<SysMenu>> listByPid(Long pid) {
        return ApiResponseUtil.success(menuService.listByPid(pid));
    }

    @Schema(description = "查询所有菜单树")
    @GetMapping("allTree")
    public ApiResponse<List<TreeNode<SysMenu>>> allTree() {
        return ApiResponseUtil.success(menuService.allTree());
    }

    @Schema(description = "通过角色获取分配的权限列表")
    @GetMapping("listByRoleId")
    public ApiResponse<List<SysMenu>> listByRoleId(@RequestParam Long roleId) {
        List<SysMenu> sysMenus = menuService.listByRoleId(roleId);
        return ApiResponseUtil.success(sysMenus);
    }
}
